package com.ruoyi.web.core.utils;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

import java.io.File;

public class SWFUtils {
    /**
     * 入口方法-通过此方法转换文件至swf格式
     *
     * @return 生成swf文件名
     */
    public String beginConvert(String home, String host, String port, String filename, String fileExt) {
        final String DOC = "doc";
        final String DOCX = "docx";
        final String XLS = "xls";
        final String XLSX = "xlsx";
        final String PPT = "ppt";
        final String PPTX = "pptx";
        String outputFile = "";
        if (null != filename && filename.indexOf(".") > 0) {
            int index = filename.indexOf(".");
            outputFile = filename.substring(0, index) + ".pdf";//文件名
        }
        //如果是office文档，先转为pdf文件
        if (fileExt.equals(DOC) || fileExt.equals(DOCX) ||
                fileExt.equals(XLS) || fileExt.equals(XLSX) ||
                fileExt.equals(PPT) || fileExt.equals(PPTX)) {
            office2PDF(home, host, port, filename, outputFile);//源文件，pdf文件
        }
        return fileExt;
    }

    /**
     * office文档转pdf文件
     *
     * @param sourceFile office文档绝对路径
     * @param destFile   pdf文件绝对路径
     * @return
     */
    private int office2PDF(String home, String host, String port, String sourceFile, String destFile) {
        String OpenOffice_HOME = home;
        String host_Str = host;
        String port_Str = port;
        try {
            File inputFile = new File(sourceFile);
            if (!inputFile.exists()) {
                return -1; // 找不到源文件
            }
            // 如果目标路径不存在, 则新建该路径
            File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            // 启动OpenOffice的服务
            String command = OpenOffice_HOME
                    + "/program/soffice.exe -headless -accept=\"socket,host="
                    + host_Str + ",port=" + port_Str + ";urp;\"";
            //启动OpenOffice服务命令：
            //soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

            System.out.println("###\n" + command);
            Process pro = Runtime.getRuntime().exec(command);
            // 连接openoffice服务
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(
                    Integer.parseInt(port_Str));
            connection.connect();
            // 转换
            DocumentConverter converter = new OpenOfficeDocumentConverter(
                    connection);
            converter.convert(inputFile, outputFile);//(officeFile, pdfFile);

            //查看是否pdf创建成功
            //outputFile.createNewFile();

            // 关闭连接和服务
            connection.disconnect();
            pro.destroy();

            return 0;
        } catch (Exception e) {
            System.out.println("no file");
            e.printStackTrace();
        }
        return 1;
    }
}
